Skip to content

feat(libkernel): report open, read and close notes on watched files - #66

Open
Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/7121
Open

Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/7121

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#7121, originally opened by GitamarT045T3R.

Source commit: cb750ff85695ea65ff493d321c3242407536895b. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

EVFILT_VNODE (kevent) and sceKernelAddFileEvent threw for NOTE_OPEN, NOTE_CLOSE, NOTE_CLOSE_WRITE, NOTE_READ, NOTE_REVOKE and unknown bits, and for a descriptor that is not a file, a directory, a pipe or a socket. They now follow FreeBSD 11:

Case FreeBSD 11 source Now
open vop_open_post NOTE_OPEN after a successful open of the file (open, openat, sceKernelOpen)
read vop_read_post, vop_readdir_post NOTE_READ after a successful read, readv, pread, preadv, their sceKernel* forms, AIO read or getdents/getdirentries, zero bytes included
close vop_close_post the close of the last descriptor of an open file reports NOTE_CLOSE, or NOTE_CLOSE_WRITE when it was opened for writing; dup2 onto it counts; a dup still open defers it
own descriptor closefp calls knote_fdclose first a watch registered through the closed descriptor is removed before the close is reported
NOTE_REVOKE, unknown bits vfs_kqfilter does not check fflags accepted; never reported, as no descriptor is revoked
character device no_kqfilter ENODEV (/dev/urandom, the null device)
terminal ttydev_kqfilter EINVAL

Windows and macOS have no host notification for opens, reads or closes, so these notes come from libkernel's own calls on every host: each vnode watch is recorded with the st_dev and st_ino that fstat reports, and the open, read and close paths deliver to the watches of the same file. Nothing is looked up while no watch exists. The last-descriptor test uses the open-file status that dup already shares; the write test asks the host access mode as fcntl(F_GETFL) does. A file read from app0.zar is watched for these notes only, as the archive never changes.

Gaps, in the libkernel debt entry: libc streams (fopen, fread) read through the host C runtime and raise none of these notes; other host processes neither; JIT shared memory is watched as its host file, where FreeBSD shared memory answers EINVAL.

Overlaps: boykopovar#7088 (kevent read and write on regular files) changes the VnodeWatch constructor, and boykopovar#7092 (NOTE_LOWAT) the lines next to the removed fflags check in Kqueue.cpp; both merge into this with one-line conflicts, whichever lands second.

Tested

Windows 10 Pro 22H2 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Ninja, Release, BUILD_TESTING=ON, on main e09dbc2b4:

  • guest_kqueue with two new cases. AccessNotes: open, read, read at end of file and pread of another descriptor report NOTE_OPEN then NOTE_READ; closing a duped descriptor reports nothing until the last copy closes (NOTE_CLOSE); a read-write descriptor closes with NOTE_CLOSE_WRITE; dup2 onto an open descriptor reports NOTE_CLOSE; sceKernelAddFileEvent gets NOTE_OPEN with its udata; closing the watched descriptor removes its own watch while a watch through another descriptor gets NOTE_CLOSE; /dev/urandom fails with ENODEV from both calls. DirectoryAccessNotes: open, getdents and close of a directory. FileWatch now checks that NOTE_REVOKE with an unknown bit is accepted and reports nothing. Passes 20 of 20 repeated runs.
  • Before: the same test against main's libkernel fails with EVFILT_VNODE: fflags 1048640 not implemented.
  • Full ctest -j 12: 1379 of 1382 pass. The three others are unrelated: select_tests needs symbolic link privilege on Windows, and agc_driver_f16_fma_denormal_modes and agc_driver_fma_mix_f16_denormal_modes get NVIDIA's f16 NaN (0x7fff) where they expect 0xfe00.
  • python tools/check_conventions.py --base origin/main passes.
  • Not built or run on Linux or macOS; there the device check uses isatty, and the notes come from the same shared code.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

EVFILT_VNODE and sceKernelAddFileEvent threw for NOTE_OPEN, NOTE_CLOSE,
NOTE_CLOSE_WRITE, NOTE_READ, NOTE_REVOKE and unknown bits, and for a
descriptor that is not a file, a directory, a pipe or a socket.

As FreeBSD 11's vop_open_post, vop_read_post, vop_readdir_post and
vop_close_post raise them, a successful open, read, readv, pread, preadv,
AIO read or getdents now reports NOTE_OPEN or NOTE_READ to the vnode
watches of that file, and closing the last descriptor of an open file,
dup2 onto it included, reports NOTE_CLOSE, or NOTE_CLOSE_WRITE when the
file was opened for writing. Files are matched by the st_dev and st_ino
that fstat reports, and the watches of a closed descriptor are removed
before its close is reported, as closefp calls knote_fdclose first.
NOTE_REVOKE and unknown bits are accepted, as vfs_kqfilter does not check
fflags, and a file read from app0.zar is watched for these notes only. A
character device fails with ENODEV and a terminal with EINVAL, as
no_kqfilter and ttydev_kqfilter answer.
@Thimo188 Thimo188 closed this Dec 2, 2026
@Thimo188 Thimo188 reopened this Dec 2, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants